﻿using ElectronicShoppingMall.Service.Models.Clinet;
using ElectronicShoppingMall.Service.Services.Client;
using ElectronicShoppingMall.Web.Front.DTOS;
using Extension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ElectronicShoppingMall.Web.Front.Controllers
{
    public class UserController : ApiController
    {
        private readonly ClientUserService _service;
        public UserController()
        {
            _service = new ClientUserService();
        }

        [HttpGet]
        public ClientUserModel GetUserInfo()
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                return _service.GetUserModel(ExtensionMethods.GetClientUserObject(token).UserId);
            }
            catch
            {
                return null;
            }
        }

        [HttpPatch]
        [Authorize]
        public ClientUserModel ChangeUserInfo([FromBody]UserInfoDTO info)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                UserInfoModel userInfo = new UserInfoModel
                {
                    Birthday = info.Birthday,
                    Email = info.Email,
                    Gender = info.Gender,
                    NickName = info.NickName,
                    PhoneNumber = info.PhoneNumber
                };
                _service.ChangeUserInfo(ExtensionMethods.GetClientUserObject(token).UserId, userInfo);
                return GetUserInfo();
            }
            catch
            {
                return null;
            }
        }

        [HttpGet]
        [Authorize]
        public List<DeliveryInfoDTO> GetUserDeliveryInfo([FromUri] int userId)
        {
            try
            {
                List<DeliveryInfoDTO> result = new List<DeliveryInfoDTO>();
                var deliveries = _service.GetUserDeliveryInfo(userId);
                foreach (var deliver in deliveries)
                {
                    DeliveryInfoDTO temp = new DeliveryInfoDTO
                    {
                        DetailedAddress = deliver.DetailedAddress,
                        Consignee = deliver.Consignee,
                        IsDefault = deliver.IsDefault,
                        PhoneNumber = deliver.PhoneNumber,
                        Id = deliver.Id
                    };
                    result.Add(temp);
                }
                return result;
            }
            catch
            {
                return null;
            }
        }
        [HttpPut]
        [Authorize]
        public List<DeliveryInfoDTO> SetDeliveryDefault([FromBody]dynamic deliver)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                _service.SetDeliveryDefault(userId, (int)deliver.deliverId);
                return GetUserDeliveryInfo(userId);
            }
            catch
            {
                return null;
            }
        }
        [HttpPost]
        [Authorize]
        public List<DeliveryInfoDTO> AddDelivertInfo(dynamic deliver)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                _service.AddDelivertInfo((string)deliver.Consignee, (string)deliver.DetailedAddress, (string)deliver.PhoneNumber, userId);
                return GetUserDeliveryInfo(userId);
            }
            catch
            {
                return null;
            }
        }
        [HttpDelete]
        [Authorize]
        public List<DeliveryInfoDTO> DeleteDeliveryInfo(dynamic obj)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                _service.DeleteDeliveryInfo(userId, (int)obj.deliverId);
                return GetUserDeliveryInfo(userId);
            }
            catch
            {
                return null;
            }
        }
        [HttpOptions]
        public String ClientRegister(UserInfoDTO info)
        {
            try
            {
                UserInfoModel userInfo = new UserInfoModel
                {
                    Account = info.Account,
                    Birthday = info.Birthday,
                    Email = info.Email,
                    Gender = info.Gender,
                    NickName = info.NickName,
                    PhoneNumber = info.PhoneNumber,
                    Password = info.Password
                };
                return _service.ClientRegister(userInfo);
            }
            catch
            {
                return null;
            }
        }
        [HttpGet]
        [Authorize]
        public String ModifyPassword(string oldPassword,string password)
        {
            try
            {
                string token = Request.Headers.Authorization.ToString();
                var userId = ExtensionMethods.GetClientUserObject(token).UserId;
                return _service.ModifyPassword(userId, oldPassword, password);
            }
            catch
            {
                return null;
            }
        }
    }
}
